package com.spark.sniffer.plugin;

import com.spark.sniffer.plugin.bytebuddy.AbstractJunction;
import com.spark.sniffer.plugin.match.*;
import net.bytebuddy.description.NamedElement;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.EqualityMatcher;
import net.bytebuddy.matcher.NullMatcher;

import javax.lang.model.element.TypeElement;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import static net.bytebuddy.matcher.ElementMatchers.*;

public class PluginFinder {
    private final Map<String, LinkedList<AbstractClassEnhancePluginDefine>> nameMatchDefine =
        new HashMap<String, LinkedList<AbstractClassEnhancePluginDefine>>();

    private final List<AbstractClassEnhancePluginDefine> signatureMatchDefine =
        new LinkedList<AbstractClassEnhancePluginDefine>();

    private final Map<Type, LinkedList<AbstractClassEnhancePluginDefine>> typeMatchDefine =
        new HashMap<Type, LinkedList<AbstractClassEnhancePluginDefine>>();

    public PluginFinder(List<AbstractClassEnhancePluginDefine> plugins) {
        for (AbstractClassEnhancePluginDefine plugin : plugins) {
            ClassMatch match = plugin.enhanceClass();

            if (match == null) {
                continue;
            }

            if (match instanceof NameMatch) {
                NameMatch nameMatch = (NameMatch)match;
                LinkedList<AbstractClassEnhancePluginDefine> pluginDefines =
                    nameMatchDefine.get(nameMatch.getClassName());
                if (pluginDefines == null) {
                    pluginDefines = new LinkedList<AbstractClassEnhancePluginDefine>();
                    nameMatchDefine.put(nameMatch.getClassName(), pluginDefines);
                }
                pluginDefines.add(plugin);
            } else if(match instanceof TypeMatch) {
                TypeMatch typeMatch = (TypeMatch) match;
                LinkedList<AbstractClassEnhancePluginDefine> pluginDefines =
                    typeMatchDefine.get(typeMatch.getType());
                if (pluginDefines == null) {
                    pluginDefines = new LinkedList<AbstractClassEnhancePluginDefine>();
                    typeMatchDefine.put(typeMatch.getType(), pluginDefines);
                }
                pluginDefines.add(plugin);
            } else {
                signatureMatchDefine.add(plugin);
            }
        }
    }

    public ElementMatcher<? super TypeDescription> buildMatch() {
        ElementMatcher.Junction judge = new AbstractJunction<NamedElement>() {
            @Override
            public boolean matches(NamedElement target) {
                return nameMatchDefine.containsKey(target.getActualName());
            }
        };

        judge = judge.and(not(isInterface()));


        for(Type type : typeMatchDefine.keySet()) {
            judge = judge.or(is(type));
        }

        for (AbstractClassEnhancePluginDefine define : signatureMatchDefine) {
            System.out.println(define);
            ClassMatch match = define.enhanceClass();
            if (match instanceof IndirectMatch) {
                judge = judge.or(((IndirectMatch)match).buildJunction());
            }
        }
        return new ProtectiveShieldMatcher(judge);
    }

    public List<AbstractClassEnhancePluginDefine> find(TypeDescription typeDescription) {
        List<AbstractClassEnhancePluginDefine> matchedPlugins = new LinkedList<AbstractClassEnhancePluginDefine>();
        String typeName = typeDescription.getTypeName();
        if (nameMatchDefine.containsKey(typeName)) {
            matchedPlugins.addAll(nameMatchDefine.get(typeName));
        }

        for(Type type : typeMatchDefine.keySet()) {
            if(type.getTypeName().equals(typeName)) {
                matchedPlugins.addAll(typeMatchDefine.get(type));
            }
        }

        for (AbstractClassEnhancePluginDefine pluginDefine : signatureMatchDefine) {
            IndirectMatch match = (IndirectMatch)pluginDefine.enhanceClass();
            if (match.isMatch(typeDescription)) {
                matchedPlugins.add(pluginDefine);
            }
        }

        return matchedPlugins;
    }

}
